from lxml import etree

html = """
    <div>
        <ul>
            <li class='i1'><a href='link1.html'>c语言</a></li>
            <li class='i2'><a href='link2.html'>c++</a></li>
            <li class='i3'><a href='link3.html'>python</a></li>
            <li class='i4'><a href='link4.html'>mongodb</a></li>
            <li class='i5'><a href='link5.html'>mysql</a></li>
            <li class='i6'><a href='link6.html'>lua</a></li>       
        </ul>
    </div>
    <div class='neuedu'>
        <ul>
            <li class='i1'><a href='link1.html'>aa</a></li>
            <li class='i2'><a href='link2.html'>bb</a></li>
            <li class='i3'><a href='link3.html'>cc</a></li>
            <li class='i4'><a href='link4.html'>dd</a></li>
            <li class='i5'><a href='link5.html'>ee</a></li>
            <li class='i6'><a href='link6.html'>ff</a></li>       
        </ul>
        <ul>
            <li class='i1'><a href='link1.html'>连城诀</a></li>
            <li class='i2'><a href='link2.html'>射雕英雄</a></li>
            <li class='i3'><a href='link3.html'>倚天屠龙</a></li>
            <li class='i4'><a href='link4.html'>笑傲江湖</a></li>
            <li class='i5'><a href='link5.html'>鹿鼎记</a></li>
            <li class='i6'><a href='link6.html'>大唐双龙</a></li>       
        </ul>
    </div>

    <ul class='neuedu'>
        <ul>
            <li class='i1'><a href='link1.html'>aa1</a></li>
            <li class='i2'><a href='link2.html'>bb1</a></li>
            <li class='i3'><a href='link3.html'>cc1</a></li>
            <li class='i4'><a href='link4.html'>dd1</a></li>
            <li class='i5'><a href='link5.html'>ee1</a></li>
            <li class='i6'><a href='link6.html'>ff1</a></li>       
        </ul>
    </ul>
"""

# 1. 创建xpath对象
o = etree.HTML(html)

# 2. 根据这个对象使用xpath
# 2.1 提取class为i1的a标签中内容
print(o.xpath('//li[@class="i1"]/a/text()'))

# 2.2 提取所有的a标签的href属性
# print(o.xpath('//a/@href'))

# 2.3 属性是neuedu标签下的第一个ul节点下的a节点的内容
# print(o.xpath('//div[@class="neuedu"]/ul[1]//a/text()'))
# print(o.xpath('//div[@class="neuedu"]/ul[last()-1]//a/text()'))
# r1 = o.xpath('//div[@class="neuedu"]/ul[last()-1]/li/a/text()')
# r2 = o.xpath('//ul[@class="neuedu"]//a/text()')
# r1.extend(r2)
# print(r1)

# 2.4 * 不限定节点 找到符合class为neuedu条件下的所有的a节点的内容
# print(o.xpath('//*[@class="neuedu"]//a/text()'))
